package gui;


import java.sql.Time;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import modelo.TableModelConsultasExternas;
import persistencia.ConsultasExternasDAO;
import pojos.Consultas;

public class PanelConsultasExternas extends javax.swing.JPanel {
  
    int idConsulta;//autoincremental
    Date fecha;
    Time horaEntrada;
    Time horaSalida;
    String especialidad;
    int idPA;//id de la tabla proceso asistencial
    ConsultasExternasDAO dao = new ConsultasExternasDAO();
    List<String> nombreColumnas = new LinkedList();

    public PanelConsultasExternas(VentanaPrincipal ventana) {
        initComponents();   
        nombreColumnas.add("idConsulta");
        nombreColumnas.add("fecha");
        nombreColumnas.add("hora de entrada");
        nombreColumnas.add("hora de salida");
        nombreColumnas.add("especialidad");
        nombreColumnas.add("id P.A.");
        Buscar();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPfiltros = new javax.swing.JPanel();
        choiseIdConsulta = new javax.swing.JCheckBox();
        fieldIdConsulta = new javax.swing.JTextField();
        choiseEspecialidad = new javax.swing.JCheckBox();
        fieldEspecialidad = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        btnConsultar = new javax.swing.JButton();

        jPfiltros.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Búsqueda"));
        jPfiltros.setLayout(new java.awt.GridLayout(4, 2));

        choiseIdConsulta.setText("id consulta");
        jPfiltros.add(choiseIdConsulta);
        jPfiltros.add(fieldIdConsulta);

        choiseEspecialidad.setText("especialidad");
        jPfiltros.add(choiseEspecialidad);
        jPfiltros.add(fieldEspecialidad);

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_plus.png"))); // NOI18N
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_bin.png"))); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_edit1.png"))); // NOI18N
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tabla);

        jPanel1.add(jScrollPane1);

        btnConsultar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_find.png"))); // NOI18N
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(237, 237, 237)
                        .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 772, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnConsultar)
                                .addGap(18, 18, 18)
                                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(btnEliminar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnModificar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAgregar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(btnConsultar))
                .addGap(41, 41, 41)
                .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        JDialogConsultasExternas dialogoAgregar=new JDialogConsultasExternas(1,this);
        dialogoAgregar.setVisible(true);                            
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        JDialogConsultasExternas dialogoEliminar=new JDialogConsultasExternas(2,this);
        dialogoEliminar.setVisible(true);
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        JDialogConsultasExternas dialogoModificar=new JDialogConsultasExternas(3,this);
        dialogoModificar.setVisible(true);
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        try {
            Buscar();
        } catch (Exception e) {
            System.out.println(e);
            JOptionPane.showMessageDialog(this, "No existen consultas externas");
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JCheckBox choiseEspecialidad;
    private javax.swing.JCheckBox choiseIdConsulta;
    private javax.swing.JTextField fieldEspecialidad;
    private javax.swing.JTextField fieldIdConsulta;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPfiltros;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables

    public void Buscar() {
        List<Consultas> listaConsultasExternas;
        boolean tickIdConsulta = this.choiseIdConsulta.isSelected();
        boolean tickEspecialidad = this.choiseEspecialidad.isSelected();
        String fecha,idConsulta,especialidad;
        if (tickIdConsulta==true) {
            idConsulta = this.fieldIdConsulta.getText();
        }else{
            idConsulta="%";
        }
        if (tickEspecialidad==true) {
            especialidad = this.fieldEspecialidad.getText();
        }else{
            especialidad="%";
        }
        listaConsultasExternas= this.dao.obtenListaConsultas(idConsulta,especialidad);        

        this.tabla.setModel(new TableModelConsultasExternas(listaConsultasExternas, nombreColumnas));
    }
}
